package com.yeyaomai.dksns.flowis.servlet;

import java.io.IOException;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.yeyaomai.dksns.flowis.domain.FlowRole;
import com.yeyaomai.dksns.flowis.domain.FlowUser;
import com.yeyaomai.dksns.flowis.service.FlowRoleService;
import com.yeyaomai.dksns.flowis.service.FlowUserService;

@Controller
@RequestMapping("/usermanage/*")
public class FlowUserController {

	@Autowired
	private FlowUserService flowUserService;

	@Autowired
	private FlowRoleService flowRoleService;

	@RequestMapping(value = "/toadduser")
	public ModelAndView toAddFlowUser(HttpServletRequest request) {
		List<FlowRole> flowRoleList = new ArrayList<FlowRole>();
		flowRoleList = flowRoleService.listFlowRoleByStatus();
		ModelAndView view = new ModelAndView("add-user");
		view.addObject("flowRoleList", flowRoleList);
		return view;
	}
	
	@RequestMapping(value = "/toupdatepassword")
	public ModelAndView toUpdatePassword(HttpServletRequest request) {
		HttpSession session = request.getSession(true);
		FlowUser flowUser = (FlowUser) session.getAttribute("flowUser");
		ModelAndView view = new ModelAndView("change-password");
		view.addObject("user", flowUser);
		return view;
	}
	
	@RequestMapping(value = "/updatepassword")
	public String updatePassword(FlowUser flowUser,HttpServletRequest request) {
		String newPassword = request.getParameter("newPassword");
		flowUser.setPassword(FlowUserController.MD5(newPassword));
		flowUserService.updateFlowUser(flowUser);
		return "redirect:/usermanage/listuser?number=1";
	}
	
	@RequestMapping(value = "/adduser")
	public String addFlowUser(FlowUser flowUser, HttpServletRequest request) {
		flowUser.setCreateTime(new Date());
		flowUser.setStatus(0);
		flowUser.setPassword(FlowUserController.MD5(flowUser.getPassword()));
		flowUserService.addFlowUser(flowUser);
		return "redirect:/usermanage/listuser?number=1";
	}

	@RequestMapping(value = "/listuser")
	public ModelAndView listFlowUser(HttpServletRequest request) {

		ModelAndView view = new ModelAndView("userlist");
		String number = request.getParameter("number");
		Page<FlowUser> result = flowUserService.listFlowUser(Integer
				.valueOf(number));
		view.addObject("result", result);
		view.addObject("number", number);
		return view;
	}

	@RequestMapping(value = "/isexistflowuser")
	public void isExistFlowUser(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String userName = request.getParameter("userName");
		boolean isExistFlowUser = flowUserService.isExistFlowUser(userName
				.trim());
		String result = "";
		if (isExistFlowUser) {
			result = "0";
		} else {
			result = "1";
		}
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().print(result);
	}

	@RequestMapping(value = "/delflowuser")
	public String delFlowUser(HttpServletRequest request) {
		String userId = request.getParameter("userId");
		flowUserService.deleteFlowUser(Long.valueOf(userId));
		return "redirect:/usermanage/listuser?number=1";
	}

	@RequestMapping(value = "/resetpassword")
	public String resetPassword(HttpServletRequest request) {
		String userId = request.getParameter("userId");
		String status = request.getParameter("status");
		FlowUser flowUser = new FlowUser();
		flowUser.setStatus(Integer.valueOf(status));
		flowUser.setUserId(Long.valueOf(userId));
		flowUser.setPassword(FlowUserController.MD5("123456"));
		flowUserService.updateFlowUser(flowUser);
		return "redirect:/usermanage/listuser?number=1";
	}
	
	@RequestMapping(value = "/updatestatus")
	public String updateStatus(HttpServletRequest request) {
		String userId = request.getParameter("userId");
		String status = request.getParameter("status");
		FlowUser flowUser = new FlowUser();
		flowUser.setUserId(Long.valueOf(userId));
		flowUser.setStatus(Integer.valueOf(status));
		flowUserService.updateFlowUser(flowUser);
		return "redirect:/usermanage/listuser?number=1";
	}
	
	@RequestMapping(value = "/toupdateflowuser")
	public ModelAndView toUpdateFlowUser(HttpServletRequest request) {
		String userId = request.getParameter("userId");
		FlowUser flowUser = flowUserService.getFlowUserByUserId(Long
				.valueOf(userId));
		ModelAndView view = new ModelAndView("user_edit");
		List<FlowRole> flowRoleList = new ArrayList<FlowRole>();
		flowRoleList = flowRoleService.listFlowRoleByStatus();
		FlowRole flowRole = new FlowRole();
		flowRole = flowRoleService.getFlowRoleUserByUserId(Long.valueOf(userId));
		view.addObject("flowRoleList", flowRoleList);
		view.addObject("user", flowUser);
		view.addObject("role", flowRole);
		return view;
	}
	
	@RequestMapping(value = "/updateuser")
	public String updateFlowUser(FlowUser flowUser, HttpServletRequest request) {
		
		//flowUser.setStatus(0);
		flowUserService.updateFlowUser(flowUser);
		return "redirect:/usermanage/listuser?number=1";
	}
	
	
	public final static String MD5(String s) {
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
				'a', 'b', 'c', 'd', 'e', 'f' };
		try {
			byte[] strTemp = s.getBytes();
			MessageDigest mdTemp = MessageDigest.getInstance("MD5");
			mdTemp.update(strTemp);
			byte[] md = mdTemp.digest();
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(str);
		} catch (Exception e) {
			return null;
		}
	}
	
	@RequestMapping(value = "/encryption")
	public void encryption(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		String password = request.getParameter("password");
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().print(FlowUserController.MD5(password));
	}
}
